package nio.Bchannel;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @author 帅小伙
 * @date 2022/1/14
 * @description
 * 读文件中的数据
 */
public class FileChannel02 {


    public static void main(String[] args) throws Exception {
        // 创建一个输入流
        File file = new File("file1.txt");
        FileInputStream fileInputStream = new FileInputStream(file);

        // 获取fileInputStream的通道
        FileChannel fileChannel = fileInputStream.getChannel();

        // 创建一个缓冲区
        ByteBuffer byteBuffer = ByteBuffer.allocate((int)file.length());

        // 将通道的数据放入buffer
        fileChannel.read(byteBuffer);

        //缓存区数据转成字符串
        System.out.println(new String(byteBuffer.array()));

    }
}
